package com.xhis.homenurse.models;

import com.xhis.homenurse.GLobalApp;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * Created by 张兵 on 2019/7/16.
 */
public abstract class Base_in
{
    /**
     * 业务功能代码
     */
    public String functioncode;

    /**
     * 验证码
     */
    public String yzm;

    /**
     * 时间
     */
    public String time;
    /**
     * 新系统验证码
     */
    public String token = "";

    /**
     * 将内容转换成xml字符串 以便之后发送xml请求；
     *
     * @throws Exception
     */
    public String getInputData() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument(); // 由builder创建新文档

        Element root = doc.createElement("yw");

        Element element = doc.createElement("functioncode");
        element.setTextContent(this.functioncode);
        root.appendChild(element);

        element = doc.createElement("yzm");
        element.setTextContent(this.yzm);
        root.appendChild(element);

        transfer(doc, root);

        element = doc.createElement("time");
        element.setTextContent(this.time);
        root.appendChild(element);

        addElement(doc,root,"token",token);

        doc.appendChild(root);
        // 将Document对象转化为String对象；
        return GLobalApp.getString(doc);
    }

    public void addElement(Document doc, Element root, String tag, String value) {
        Element element = doc.createElement(tag);
        element.setTextContent(value);
        root.appendChild(element);
    }

    /**
     * 子类项目的转换
     *
     * @param doc
     * @param root
     */
    protected abstract void transfer(Document doc, Element root);
}
